#!/usr/bin/python
#encoding:utf-8
'''
Created on 2013-7-18
 
@author: ZetaChoww
'''
import cookielib, urllib, urllib2,json
from file2json import *
 
 
baseURL="http://localhost:8080/drupal/?q=%s"
#########################################################################
loginURL ='rest/user/login' 
sessionTokenURL ='services/session/token'
 
CSRF=None
##########################################################################
 
cookiejar = cookielib.CookieJar()
urlOpener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
Movie_Class_To_Num = {'爱情':'1','动作':'2','犯罪':'3','励志':'4','喜剧':'6','科幻':'7','恐怖':'8','悬疑':'9','文艺':'10','剧情':'11','灾难':'12','动漫':'13','综艺':'14'} 
 
##########################################################################
 
def goRequest(url='',values={},method='GET'):
    data = json.dumps(values)
    request = urllib2.Request(baseURL%(url),data)
    request.add_header("Content-Type", "application/json;charset=utf-8")
    if CSRF!=None:request.add_header("X-CSRF-Token", CSRF)
    request.get_method = lambda:method
    url=urlOpener.open(request)
    result = url.read()
    return result.encode('utf-8')
 
 
def sync_node(nodeinfo=None):
    #print 'nodeinfo',nodeinfo
    try:
        #modify some field from license info on Gzjjzd database
        #return json.dumps(json.loads(goRequest('rest/file',nodeinfo ,'POST')), ensure_ascii=False)
        return goRequest('rest/file',nodeinfo ,'POST')
    except Exception,e:
        print e
      
if __name__=='__main__':
    fp = open('movielist.txt')
    movielist = fp.read()
    for movie in movielist:

def upload_movie(amovie):
    goRequest(loginURL,{'username':'qing','password':'qing2drupal'},'POST')
    CSRF=goRequest(sessionTokenURL)
    fp = open('movie.json')
    movie_js = json.load(fp)
    fp.close()

    movie_js['Title'] = amovie['标题']
    movie_js['Body'] = amovie['简介']
    movie_js['field_grade']['und'][0]['rating']=amovie['评分']
    movie_js['field_classe']['und']['tid']= Movie_Class_To_Num[amovie['类型'].split(' / ')[0]]
    movie_js['field_classe2']['und']['tid']= Movie_Class_To_Num[amovie['类型'].split(' / ')[1]]
    movie_js['field_runtime']['und'][0]['value']=amovie['片长']
    movie_js['field_realeased_date']['und'][0]['value']=amovie['上映日期']
    movie_js['field_region']['und'][0]['value']=amovie['制片国家/地区']
    #movie_js['field_english_name']['und'][0]['value']=amovie['英文名']
    movie_js['field_director']['und'][0]['value']=amovie['导演']
    #split_heros = amovie['主演'].split('&')
    #for i in range(len(split_heros)):
    movie_js['field_heros']['und'][0]['value']=amovie['主演']
    pic1 = sync_node((image2json(amovie['电影截图1'])))
    pic2 = sync_node((image2json(amovie['电影截图2'])))
    pic3 = sync_node((image2json(amovie['电影截图3'])))
    poster = sync_node((image2json(amovie['电影海报1'])))
    slideshow = sync_node((image2json(amovie['电影海报2'])))
    movie_js['field_pictures']['und'][0]['fid']=json.loads(pic1)['fid']
    movie_js['field_pictures2']['und'][0]['fid']=json.loads(pic2)['fid']
    movie_js['field_pictures3']['und'][0]['fid']=json.loads(pic3)['fid']
    movie_js['field_poster']['und'][0]['fid']=json.loads(poster)['fid']
    movie_js['field_slideshow']['und'][0]['fid']=json.loads(slideshow)['fid']
    movie_js['field_download_links']['und'][0]['value'] +=(amovie['下载地址'])
    print json.dumps(movie_js)
    #exit()
    #node = image2json('1347255992750.jpg')
    #node = torrent2json('[2002][中国][演唱会][王菲全面体演唱会][720P-4.16G][ISO].torren.torrent')
    #node = movie2json('name')
    print goRequest('rest/node',movie_js,'POST')
         
    #close the request opener
    urlOpener.close()

